package it.poste.il.compensatore.dao.impl;

import it.poste.il.compensatore.dao.MessageDao;
import it.poste.il.compensatore.entity.Message;
import java.util.List;

import javax.persistence.NoResultException;

import org.springframework.transaction.annotation.Transactional;

public class MessageDaoImpl extends BaseEntityDaoImpl<Message> implements MessageDao {
    public MessageDaoImpl() {
        super(Message.class, "Message");
    }

    public List<Message> loadByFilter(Message entityFilter) {
        throw new UnsupportedOperationException();
    }

    public Message loadByExample(Message entity) {
        throw new UnsupportedOperationException();
    }

//	@Transactional
//	public int selectMessageId(Message msg) {
//		try {
//	      return (int) getEntityManager().createNamedQuery("SELECT msg.id FROM Messages msg " +
//              "WHERE msg.requestid = :requestId AND msg.operationid = :operationId").
//              setParameter("requestId", msg.getRequestId()).
//              setParameter("operationId", msg.getOperationId()).
//              getSingleResult();
//	    } catch (NoResultException e) {
//	      return 0;
//	    }
//	}
	
	@Transactional
	public int selectOperationId(Message msg) {
		try {
		      return (int) getEntityManager().createNamedQuery("SELECT msg.operationid_id FROM Messages msg " +
	              "WHERE msg.id = :id AND msg.type = :type").
	              setParameter("id", msg.getId()).
	              setParameter("type", msg.getType()).
	              getSingleResult();
		} catch (NoResultException e) {
		      return 0;
		}
	}
	
	@Transactional
	public int selectStatusId(Message msg) {
		try {
		      return (int) getEntityManager().createNamedQuery("SELECT msg.statusId_id FROM Messages msg " +
		    		  "WHERE msg.id = :id AND msg.type = :type").
		              setParameter("id", msg.getId()).
		              setParameter("type", msg.getType()).
	              getSingleResult();
		} catch (NoResultException e) {
		      return 0;
		}
	}
	
	@Transactional
	public int loadLastMessage(Message message){
		try{
			return (int) getEntityManager().createNamedQuery("SELECT msg.statusId_id FROM Messages msg,  " +
		    		  "WHERE msg.id = :id AND msg.type = :type").
		              setParameter("id", message.getId()).
		              setParameter("type", message.getType()).
	              getSingleResult();
		} catch (NoResultException e) {		
			return  0;
		}
	}
}
